package com.acme.bankapp.domain.bank;
import com.acme.bankapp.service.bank.ClientRegistrationListener;


public class Bank {
	
	private int num;
	private int maxNumClients;
	private int maxNumListener;
	private Client[] clients;
	private ClientRegistrationListener [] listeners;
	
	public Bank(int maxNumClients, int maxNumListener, Client[] clients) {
		num = 0;
		this.maxNumClients = maxNumClients;
		this.maxNumListener = maxNumListener;
		this.clients = clients;
		listeners = new ClientRegistrationListener[maxNumListener];	
		
		if (maxNumListener < 2) {
			System.out.println("the wrong number of listeners");
			return;
		}
		
		listeners[0] = new ClientRegistrationListener() {		   
			   
			public void onClientAdded(Client c) {
			   System.out.println("New client " + c.getClientSalutation() + ", welcomoe!");
			}
		 };	
			   
		 listeners[1] = new ClientRegistrationListener() {
					   
			  public void onClientAdded(Client c) {
					System.out.println("Notification email for client " + c.getClientSalutation() +
				" to be sent");
			  }		
		};		
	}
	
	public Bank() {
		this(10, 2, new Client [2]);
	}
	
	public int getMaxNumClients() {
		return maxNumClients;
	}
	
	public int getNumClients() {
		return num;
	}
	
	public Client getClient (int num) {
		if (num > this.num) {
			return null;
		}
		return clients[num]; 
	}	
	
	public int addClient (Client client) {
		if (num >= maxNumClients) {
			System.out.println("too many clients");
			return 1;			
		} 
		
		for (int i = 0; i < maxNumListener; i++) {
			listeners[i].onClientAdded(client);
		}
		
		clients[num] = client;
		num++;
		return 0;
	}

}
